//
//  NSManagedObject+DF.m
//  微商社区
//
//  Created by nahuo on 15/1/6.
//  Copyright (c) 2015年 nahuo. All rights reserved.
//

#import "NSManagedObject+DF.h"

@implementation NSManagedObject (DF)

+ (id)syncWithKey:(NSString*)key value:(id)value context:(NSManagedObjectContext*)context {
    
    NSString* entityName = NSStringFromClass(self);
    NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:entityName];
    [request setPredicate:[NSPredicate predicateWithFormat:@"%@ == %@",key,value]];
    id  object =  [[context executeFetchRequest:request error:nil] firstObject];
    if(object) {
        return object;
    }else {
        object = [NSEntityDescription insertNewObjectForEntityForName:entityName inManagedObjectContext:context];
        
        [object setValue:value forKey:key];
        return object;
    }
}

@end
